/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.osiris.persistence.controllers.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.osiris.persistence.Requisito;
import org.osiris.persistence.Riesgo;
import org.osiris.persistence.controllers.RiesgoInterface;
import org.osiris.persistence.controllers.exceptions.NonexistentEntityException;

/**
 *
 * @author amartinez
 */
public class RiesgoJpaController implements RiesgoInterface{

    public RiesgoJpaController() {
        emf = CreadorPU.getEntityManagerFactory();
    }
    private EntityManagerFactory emf = null;

    /* (non-Javadoc)
	 * @see org.osiris.persistence.controllers.AsignacionesInterface#getEntityManager()
	 */
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Riesgo riesgo) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Requisito idRequisito = riesgo.getIdRequisito();
            if (idRequisito != null) {
                idRequisito = em.getReference(idRequisito.getClass(), idRequisito.getIdRequisito());
                riesgo.setIdRequisito(idRequisito);
            }
            em.persist(riesgo);
            if (idRequisito != null) {
                idRequisito.getRiesgoList().add(riesgo);
                idRequisito = em.merge(idRequisito);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Riesgo riesgo) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Riesgo persistentRiesgo = em.find(Riesgo.class, riesgo.getIdRiesgo());
            Requisito idRequisitoOld = persistentRiesgo.getIdRequisito();
            Requisito idRequisitoNew = riesgo.getIdRequisito();
            if (idRequisitoNew != null) {
                idRequisitoNew = em.getReference(idRequisitoNew.getClass(), idRequisitoNew.getIdRequisito());
                riesgo.setIdRequisito(idRequisitoNew);
            }
            riesgo = em.merge(riesgo);
            if (idRequisitoOld != null && !idRequisitoOld.equals(idRequisitoNew)) {
                idRequisitoOld.getRiesgoList().remove(riesgo);
                idRequisitoOld = em.merge(idRequisitoOld);
            }
            if (idRequisitoNew != null && !idRequisitoNew.equals(idRequisitoOld)) {
                idRequisitoNew.getRiesgoList().add(riesgo);
                idRequisitoNew = em.merge(idRequisitoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = riesgo.getIdRiesgo();
                if (findRiesgo(id) == null) {
                    throw new NonexistentEntityException("The riesgo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Riesgo riesgo;
            try {
                riesgo = em.getReference(Riesgo.class, id);
                riesgo.getIdRiesgo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The riesgo with id " + id + " no longer exists.", enfe);
            }
            Requisito idRequisito = riesgo.getIdRequisito();
            if (idRequisito != null) {
                idRequisito.getRiesgoList().remove(riesgo);
                idRequisito = em.merge(idRequisito);
            }
            em.remove(riesgo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Riesgo> findRiesgoEntities() {
        return findRiesgoEntities(true, -1, -1);
    }

    public List<Riesgo> findRiesgoEntities(int maxResults, int firstResult) {
        return findRiesgoEntities(false, maxResults, firstResult);
    }

    private List<Riesgo> findRiesgoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Riesgo as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Riesgo findRiesgo(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Riesgo.class, id);
        } finally {
            em.close();
        }
    }

    public int getRiesgoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Riesgo as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
